package com.example.fresh_fruit.service.Impl;

import com.example.fresh_fruit.entity.Customer;
import com.example.fresh_fruit.mapper.CustomerMapper;
import com.example.fresh_fruit.service.CustomerService;
import com.example.fresh_fruit.util.CheakMd5;
import com.example.fresh_fruit.util.ResultCodeEnum;
import com.example.fresh_fruit.第三方接口.Recharge;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :飞天の鱼
 * @date : 2022/8/12 11:11
 */
@Service
//service层业务实现需要添加注解@Service，注入Mapper：
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public boolean deleteCustomer(Integer id) {
        return false;
    }

    @Override
    public Integer updeCustomer(Integer id, String username, String oldPasswd, String newPasswd) {
        // 调用findById()查询用户数据
        Customer result = customerMapper.findById(id);
        if (result == null) {
            return 1;
            //修改密码失败，尝试访问的用户数据不存在
        }
        if (result.getState() == 1) {
            return 2;
//            修改密码失败，用户数据已被删除！
        }
        //输入的老密码进行加密，与数据库中查询到的密码比较
        String oldPasswd1 = CheakMd5.getMd5(oldPasswd);
        String passwd = result.getPasswd();
        if (!oldPasswd1.equals(passwd)) {
            return 3;
            //密码错误，请输入正确的密码
        }
        // 日志
        System.err.println("\t验证通过，更新密码：");
        String newPasswd1 = CheakMd5.getMd5(newPasswd);
        result.setPasswd(newPasswd1);
        customerMapper.update(result);
        return 0;
    }

    @Override
    public Customer findId(Integer id) {
        return null;
    }

    @Override
    public List<Customer> selectAll() {
        return customerMapper.selectAll();
    }

    @Override
    public Customer findByName(String name) {
        Customer byName = customerMapper.findByName(name);
        System.out.println(byName);
        return byName;
    }

    @Override
    public int insertCustomer(Customer customer) {//注册,插入
        String username = customer.getName();
        Customer findByCustomer = customerMapper.findByName(username);

        if (findByCustomer != null) {
            //用户名已存在
            return 1;
        } else if (findByCustomer == null) {
            String passwd = customer.getPasswd();
            String passwd1 = CheakMd5.getMd5(passwd);
            customer.setPasswd(passwd1);
            customerMapper.insertName(customer);
        }
        return 0;
    }


    @Override
    public Customer login(String username, String passwd) {//登入
        Customer customer1 = new Customer();
        Customer customer = customerMapper.findByName(username);
        if (customer == null) {
            System.out.println(ResultCodeEnum.FETCH_USERINFO_ERROR);
        } else {
            //检测用户密码
            // 数据库中查询到的密码
            String oldPasswd = customer.getPasswd();
            //前端传来的密码
            String newPasswd = CheakMd5.getMd5(passwd);
            if (oldPasswd.equals(newPasswd)) {
                customer1 = customerMapper.login(username, oldPasswd);
                return customer1;
            } else {
                System.out.println("error");
                return null;
            }
        }
        return customer1;
    }

    @Override
    public int updataBanlance(Recharge recharge, Integer id) {
        //要充值的钱
        Double rechargeNumber = recharge.getRechargeNumber();
        Customer customerMapperById = customerMapper.findById(id);
        if (customerMapperById == null) {
            log.info("用户为空");
            return 1;
        }
        double accountBanlance = customerMapperById.getAccountBanlance();
        double banlanceMoney = rechargeNumber + accountBanlance;
        customerMapperById.setAccountBanlance(banlanceMoney);
        int i = customerMapper.updataBanlance(id);
        if (i == 0) {
            return 2;
        } else {
            return 0;
        }
    }

}
